// pages/teacherattendance/teacherattendance.js
const util = require('../../utils/util.js')
const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    attendanceDays: 0,
    attendanceDaysforLeave: 0,
    noattendanceDays: 0,
    lateattendanceDays: 0,
    leaveerralyDays: 0,
    leavedateList: null,
    startdate: util.formatDateMonth(new Date()),
    enddate: util.formatDateMonth(new Date()),
    attendancedateList: null,
    date: util.formatDate(new Date()),
    currentdate: util.formatDate(new Date()),
    currentyear: new Date().getFullYear(),
    currentmonth: new Date().getMonth() + 1, // 月份
    currentday: new Date().getDate(),
    year: new Date().getFullYear(), // 年份
    month: new Date().getMonth() + 1, // 月份
    day: new Date().getDate(),
    str: MONTHS[new Date().getMonth()], // 月份字符串
    demo4_days_style: [],
    list: null,
    ru: '../../images/icon_ru.png',
    li: '../../images/icon_li.png',
    card: '../../images/icon_card.png',
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    console.log(this.data.date)
    console.log(this.data.startdate)
    console.log(this.data.enddate)
    // this.setDateColor(this);
    this.queryData();
  },
  next: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.currentYear,
      month: event.detail.currentMonth,
      day: 1,
      date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
    })
    this.queryData();

  },

  prev: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.currentYear,
      month: event.detail.currentMonth,
      day: 1,
      date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
    })
    this.queryData();
  },

  dateChange: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.currentYear,
      month: event.detail.currentMonth,
      day: 1,
      date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
    })
    this.queryData();
  },

  dayClick: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.year,
      month: event.detail.month,
      day: event.detail.day,
      date: util.formatDate(new Date(event.detail.year + "-" + util.formatNumber(event.detail.month) + "-" + util.formatNumber(event.detail.day))),
    })
    this.queryData();
  },


  setDateColor: function (that) {
    const days_count = new Date(that.data.year, that.data.month, 0).getDate();
    let demo4_days_style = new Array;
    for (let i = 1; i <= days_count; i++) {
      var flag = 0;
      if (that.data.leavedateList != null) {
        for (let j = 0; j < that.data.leavedateList.length; j++) {
          var a = that.data.leavedateList[j].date;
          var days = parseInt(a.substring(8, 10));
          if (i == days) {
            flag = 1
            demo4_days_style.push({
              month: 'current',
              day: i,
              color: 'white',
              background: '#f3d347'
            });
            break;
          }
        }
      }
      if (that.data.attendancedateList != null) {
        for (let j = 0; j < that.data.attendancedateList.length; j++) {
          var a = that.data.attendancedateList[j].date;
          var days = parseInt(a.substring(8, 10));
          if (i == days) {
            flag = 1
            demo4_days_style.push({
              month: 'current',
              day: i,
              color: 'white',
              background: '#27c868'
            });
            break;
          }
        }
      }
      if (flag == 0) {

        if (that.data.year == that.data.currentyear && that.data.month == that.data.currentmonth) {
          if (i < that.data.currentday) {
            demo4_days_style.push({
              month: 'current',
              day: i,
              color: 'white',
              background: '#fc4a5a'
            });
          } else {
            demo4_days_style.push({
              month: 'current',
              day: i,
              color: '#27c868',
              background: 'white'
            });
          }

        } else {
          if ((that.data.year < that.data.currentyear) || that.data.month < that.data.currentmonth) {
            demo4_days_style.push({
              month: 'current',
              day: i,
              color: 'white',
              background: '#fc4a5a'
            });
          }

        }
      }

    }

    demo4_days_style.push({
      month: 'current',
      day: this.data.day,
      color: 'black',
      background: 'yellow'
    });

    that.setData({
      demo4_days_style: demo4_days_style
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.queryData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  previewImg: function (e) {
    var index = e.target.dataset.index;
    var current = e.target.dataset.src;
    var that = this;
    var imgArr = [];

    var url1 = that.data.list[index].PunchPhoto
    if (url1 != null && url1.length > 0) {
      imgArr.push(url1);
    }


    var url = that.data.list[index].PunchPic;
    if (url != null && url.length > 0) {
      imgArr.push(url);
    }

    wx.previewImage({
      current: current, //当前图片地址
      urls: imgArr, //所有要预览的图片的地址集合 数组形式
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  queryData: function (e) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "date": that.data.date,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_attendance"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);

        if (res.data.status == 0) {
          that.setData({
            leavedateList: res.data.leavedateList,
            attendancedateList: res.data.attendancedateList,
            attendanceDays: res.data.attendanceDays,
            attendanceDaysforLeave: res.data.attendanceDaysforLeave,
            noattendanceDays: res.data.noattendanceDays,
            list: res.data.data,
            lateattendanceDays: res.data.lateattendanceDays,
            leaveerralyDays: res.data.leaveerralyDays,
            startdate: util.formatDateMonth(new Date(res.data.leftDay.substring(0, 7))),
            list: res.data.data
          })
          that.setDateColor(that);
        }
      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },
  addLeave: function (e) {
    wx.navigateTo({
      url: '../qingjia/qingjia'
    })
  }
})